package cn.harry.cabinet.repository

import android.app.Application
import cn.harry.cabinet.database.AppDatabase
import cn.harry.cabinet.model.entity.HardwareDevice
import cn.harry.cabinet.model.enums.DeviceType
import java.util.Date

/**
 * 硬件设备配置仓库
 */
class HardwareDeviceRepository(application: Application) : BaseRepository() {

    private val hardwareDeviceDao = AppDatabase.getDatabase(application).hardwareDeviceDao()


    /**
     * 获取所有设备配置（非LiveData）
     */
    suspend fun getAllDevicesList(): List<HardwareDevice> {
        return hardwareDeviceDao.getAllDevicesList()
    }

    /**
     * 根据设备类型获取设备配置
     */
    suspend fun getDeviceByType(deviceType: DeviceType): HardwareDevice? {
        return hardwareDeviceDao.getDeviceByType(deviceType.name)
    }

    /**
     * 根据设备类型获取设备配置（LiveData）
     */
    fun getDeviceByTypeLive(deviceType: DeviceType) = 
        hardwareDeviceDao.getDeviceByTypeLive(deviceType.name)

    /**
     * 获取所有启用的设备配置（非LiveData）
     */
    suspend fun getEnabledDevicesList(): List<HardwareDevice> {
        return hardwareDeviceDao.getEnabledDevicesList()
    }

    /**
     * 插入设备配置
     */
    suspend fun insert(device: HardwareDevice): Long {
        return hardwareDeviceDao.insert(device)
    }

    /**
     * 批量插入设备配置
     */
    suspend fun insertAll(devices: List<HardwareDevice>) {
        hardwareDeviceDao.insertAll(devices)
    }

    /**
     * 更新设备配置
     */
    suspend fun update(device: HardwareDevice) {
        hardwareDeviceDao.update(device.copy(updateTime = Date()))
    }

    /**
     * 删除设备配置
     */
    suspend fun delete(device: HardwareDevice) {
        hardwareDeviceDao.delete(device)
    }

}
